package demoServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MyServlet extends HttpServlet {

	@Override    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {    
		
		
     // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "HTTP Header 请求实例 - 菜鸟教程实例";
        String docType =
            "<!DOCTYPE html> \n";
            out.println(docType +
            "<html>\n" +
            "<head><meta charset=\"utf-8\"><title>" + title + "</title></head>\n"+
            "<body bgcolor=\"#f0f0f0\">\n" +
            "<h1 align=\"center\">" + title + "</h1>\n" +
            "<table width=\"100%\" border=\"1\" align=\"center\">\n" +
            "<tr bgcolor=\"#949494\">\n" +
            "<th>Header 名称</th><th>Header 值</th>\n"+
            "</tr>\n");

        Enumeration headerNames = request.getHeaderNames();

        while(headerNames.hasMoreElements()) {
            String paramName = (String)headerNames.nextElement();
            out.print("<tr><td>" + paramName + "</td>\n");
            String paramValue = request.getHeader(paramName);
            out.println("<td> " + paramValue + "</td></tr>\n");
        }
        out.println("</table>\n</body></html>");
    }    
    
    @Override    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)    
            throws ServletException, IOException {    
        System.out.println("doPost MyServlet");
        System.out.println("输出数据"+req.getParameter("name"));
        super.doPost(req, resp);    
    }    
      
    @Override  
    public void init(ServletConfig servletConfig) throws ServletException {  
        System.out.println("MyServlet"); 
    }  
    
    @Override
    public void destroy() {
        System.out.println("servlet销毁！");
        super.destroy();
    }

}
